import {
	createRouter,
	createWebHistory
} from 'vue-router';

import Home from '@/views/home';
import Search from '@/views/search';
import NotFound404 from '@/views/404';

import galleryRoutes from './gallery';
import workshopRoutes from './workshop';
import acFunRoutes from './acFun';

const baseRouteTitle = 'lulu';

const baseRoutes = [{
	path: '/search',
	name: 'search',
	component: Search,
	meta: {
		title: '搜索'
	}
}, {
	path: '/',
	alias: '/home',
	name: 'home',
	component: Home,
	meta: {
		title: '首页'
	}
}, {
	name: '404',
	path: '/:pathMatch(.*)*',
	component: NotFound404,
	meta: {
		title: 'NotFound 404'
	}
}]

const router = createRouter({
	history: createWebHistory(),
	routes: [
		...galleryRoutes,
		...workshopRoutes,
		...acFunRoutes,
		...baseRoutes
	],
	// linkActiveClass: '',
	// linkExactActiveClass: '',
	scrollBehavior(to, from, savedPosition) {
		if (savedPosition) {
			return savedPosition;
		} else {
			return { top: 0 };
		}
	}
})

router.beforeEach((to, from) => {
	
	document.title = to.matched.reduce((last, next) => {
		return last + (next.meta.title ? ` - ${next.meta.title}` : '' );
	}, baseRouteTitle);
	
})

export default router;
